//
//  FindViewController.m
//  Anyair
//
//  Created by karlcool on 14-5-13.
//  Copyright (c) 2014年 Karlcool. All rights reserved.
//

#import "FindViewController.h"
#import "Regular.h"
@interface FindViewController ()

@end

@implementation FindViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    self.view.backgroundColor = AIR_GRAY;
    
    LRTopBar *topBar = [[LRTopBar alloc]initWithTitle:@"修改密码" AndLeftText:@"取消" AndRightText:@"确认"];
    topBar.delegete = self;
    [self.view addSubview:topBar];
    
    UIView *passwordBackView = [[UIView alloc]initWithFrame:CGRectMake(-0.5, 100, 321, 100)];
    passwordBackView.backgroundColor = [UIColor whiteColor];
    passwordBackView.layer.borderColor = BREAKLINE_GRAY.CGColor;
    passwordBackView.layer.borderWidth = 0.5;
    [self.view addSubview:passwordBackView];
    
    passwordText = [[UITextField alloc]initWithFrame:CGRectMake(25, 10, 245, 30)];
    passwordText.backgroundColor = [UIColor clearColor];
    passwordText.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
    passwordText.placeholder = @"密码";
    passwordText.secureTextEntry = YES;
    [passwordText addTarget:self action:@selector(didEndExit:) forControlEvents:UIControlEventEditingDidEndOnExit];
    [passwordBackView addSubview:passwordText];
    
    
    surePasswordText = [[UITextField alloc]initWithFrame:CGRectMake(25, 60, 245, 30)];
    surePasswordText.backgroundColor = [UIColor clearColor];
    surePasswordText.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
    surePasswordText.placeholder = @"确认密码";
    surePasswordText.secureTextEntry = YES;
    [surePasswordText addTarget:self action:@selector(didEndExit:) forControlEvents:UIControlEventEditingDidEndOnExit];
    [passwordBackView addSubview:surePasswordText];
    
    
    UIView *breakLine3 = [[UIView alloc]initWithFrame:CGRectMake(25, 50, 295, 1)];
    breakLine3.backgroundColor = BREAKLINE_GRAY;
    [passwordBackView addSubview:breakLine3];
}

#pragma mark - 释放键盘
- (void)didEndExit:(UITextField *)sender {
    [sender resignFirstResponder];
}

#pragma mark - 顶栏委托

#pragma mark - 取消验证
- (void)clickLeft:(UIButton*)sender {
    [self dismissViewControllerAnimated:YES completion:nil];
    
}

#pragma mark - 载入找回密码
- (void)clickRight:(UIButton*)sender {
    if (![Regular validatePassword:passwordText.text]) {
        UIAlertView *passwordAlert = [[UIAlertView alloc]initWithTitle:@"错误" message:@"密码必须为6到16为数字和字母" delegate:nil cancelButtonTitle:@"好的" otherButtonTitles:nil];
        [passwordAlert show];
        return;
    }
    if (![passwordText.text isEqualToString:surePasswordText.text]) {
        UIAlertView *passwordAlert = [[UIAlertView alloc]initWithTitle:@"错误" message:@"两次密码不一致" delegate:nil cancelButtonTitle:@"好的" otherButtonTitles:nil];
        [passwordAlert show];
        return;
    }
    if (![LRTools checkNetWork]) {
        UIAlertView *failAlert = [[UIAlertView alloc]initWithTitle:@"网络不可用" message:@"请检查你的网络" delegate:nil cancelButtonTitle:@"好的" otherButtonTitles:nil];
        [failAlert show];
        return;
    }

    //修改密码操作
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    dispatch_async (dispatch_get_global_queue (DISPATCH_QUEUE_PRIORITY_LOW ,0),^{
        NSDictionary *resposeDic = [[LRHttpManager createHttpManager]resetPasswordByPhone:self.userPhone AndNewPassword:passwordText.text AndVerifycode:self.authCode];
        
        dispatch_async (dispatch_get_main_queue(),^{
            
            if ([[resposeDic objectForKey:@"code"] integerValue]==0) {
                [MBProgressHUD hideAllHUDsForView:self.view animated:YES];
                MBProgressHUD *shortHub = [[MBProgressHUD alloc]initWithView:self.view];
                [self.view addSubview:shortHub];
                [shortHub setLabelText:@"修改密码成功"];
                [shortHub setMode:MBProgressHUDModeText];
                [shortHub hide:YES afterDelay:1];
                [shortHub show:YES];
                [[NSUserDefaults standardUserDefaults]setObject:[NSDate date] forKey:@"lastAuthTime"];
                [self.navigationController popToRootViewControllerAnimated:YES];
            } else {
                [MBProgressHUD hideAllHUDsForView:self.view animated:YES];
                MBProgressHUD *shortHub = [[MBProgressHUD alloc]initWithView:self.view];
                [self.view addSubview:shortHub];
                [shortHub setLabelText:@"修改密码失败,请稍后再试"];
                [shortHub setMode:MBProgressHUDModeText];
                [shortHub hide:YES afterDelay:1];
                [shortHub show:YES];
            }
            
        });
    });
    
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
